local CommonOHOH = _G.CommonOHOH;

local monster_cfg = CommonOHOH.config.Monster
local levels_cfg = CommonOHOH.config.Levels

local M={}


function createMId(lv,cid)
    return lv.."_"..cid
end

function ScaleNum(scale,base)
   return math.floor(scale*base)
end


-- function GetToNumber(num)

--     local num= math.floor(num)
    
--     local str=tostring(num)

--     if string.len(str)<=3 then
--         return num
--     end

--     local subStr=string.sub(str,1,3)
--     local r=tonumber(subStr)
--     for i=1,string.len(str)-2 do
--         r=r*10
--     end
--     return r
-- end


function AddNum(base,add)
    return  base+add
        -- return math.floor(scale*base)
 end

 

local key2Scale={
    ["base_wins"]="winsScale",
    ["base_meat"]="meatScale",
    ["base_diamond"]="diamondScale",
}

local gMonsterCfg={}

for id,v in pairs(levels_cfg) do
    for cid,j in pairs(monster_cfg) do
        local mid=createMId(id,cid)
        local cfg=CommonOHOH:DeepCopy(j)
        cfg["id"]=mid
        cfg["perfab"]=v.perfab[cid]
        cfg["hp"]=ScaleNum(v.hpScale,j.hp)
        -- cfg["hp"]=AddNum(v.baseHp,j.hp)
        cfg["attack"]=ScaleNum(v.attackScale,j.attack)
        cfg["meat"]=ScaleNum(v.meatScale,j.meat)
        if j.drop then
            local drop={}
            for k,d in pairs(j.drop) do
                drop[k]=ScaleNum(v[key2Scale[k]],d)
            end
            cfg["drop"]=drop
        end

        gMonsterCfg[mid]=cfg
    end
end




-- --通过怪物Id获取 刷怪索引和光卡
-- function M.GetCfgIdAndLevel(id)
--     local list=  string.split(id,"_")
--     return tonumber(list[1]),tonumber(list[2])
-- end

  --生成怪物配置Id
function M.GetMId(lv,cid)
    return createMId(lv,cid)
end

function M.GetCfgByLvIdAndCid(lvId,cid)
    local mId=createMId(lvId,cid)
    return gMonsterCfg[mId]
end

function M.GetCfgByMid(mId)
    return gMonsterCfg[mId]
end

function M.GetMonsteArtByMid(id)
    local perfabId=gMonsterCfg[id].perfab
    local Art=CommonOHOH:GetPath_Loop(perfabId,game.ReplicatedStorage.Resource.Monster)
    return {
        model=CommonOHOH:GetPath_Loop("model",Art),
        size=CommonOHOH:GetPath_Loop("size",Art).Value,
        audio={
            die=CommonOHOH:GetPath_Loop("die",Art),
            hurt=CommonOHOH:GetPath_Loop("hurt",Art),
        }
    }

end

function M.GetMonsterPerfabByMId(mid)
    return M.GetMonsteArtByMid(mid).model:Clone()
end

function M.GetSizeIdByMId(mid)
    return M.GetMonsteArtByMid(mid).size
end

return M